package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.XinWenFenLeiDaoImpl;
import com.fk.entity.XinWenFenLei;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;

public class AdminNewsCategoryServlet extends BaseServlet {
	
	/**
	 * 鏌ヨ鎵�鏈夋柊闂昏祫璁垎绫�
	 * localhost:8080/wuyijiu/adminNewsCategory?reqName=getNewsCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenFenLeiDaoImpl ncDao=new XinWenFenLeiDaoImpl();//鍒涘缓瀹炵幇绫诲璞�
		List<XinWenFenLei> ncList=ncDao.getXinWenFenLeiList();//鏌ヨ鎵�鏈夋柊闂昏祫璁垎绫诲璞￠泦鍚�
		
		response.setCharacterEncoding("UTF-8");//璁剧疆鍝嶅簲鐨勭紪鐮佹牸寮�
		PrintWriter out=response.getWriter();//out瀵硅薄
		
		Gson gson=new Gson();//Gson瀵硅薄
		String res=gson.toJson(ncList);//灏唍cList杞寲涓簀son瀛楃涓�
		
		out.print(res);//鍝嶅簲鏁版嵁
		
		out.flush();
		out.close();
	}

	
	/**
	 * 鍒犻櫎鏂伴椈璧勮鍒嗙被
	 * localhost:8080/wuyijiu/adminNewsCategory?reqName=delNewsCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewsCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int nId=Integer.parseInt(request.getParameter("xinWenFenLeiId"));//鎺ュ彈鍓嶅彴浼犳潵鐨勫弬鏁�
		XinWenFenLeiDaoImpl ncDao=new XinWenFenLeiDaoImpl();//鍒涘缓瀹炵幇绫诲璞�
		int n=ncDao.delNewsCategory(nId);//璋冪敤鍒犻櫎鐨勬柟娉�
		
		response.setCharacterEncoding("UTF-8");//璁剧疆鍝嶅簲鐨勭紪鐮佹牸寮�
		PrintWriter out=response.getWriter();//out瀵硅薄
		
		if (n>0) {//鍒犻櫎鎴愬姛
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//鍒犻櫎澶辫触
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();
	}
/**
	
	 * localhost:8080/wuyijiu/adminNewsCategory?reqName=addNewsCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addNewsCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		XinWenFenLeiDaoImpl flDao=new XinWenFenLeiDaoImpl();//产品分类的实现类对象
		
		
		
		String cateName=request.getParameter("cateName");
		XinWenFenLei fl=new XinWenFenLei(0, cateName);//根据接受的参数创建新闻分类的对象
		
		int n=flDao.addNewsCategory(fl);
		if (n>0) {//非空判定
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));
		}
		
		out.flush();
		out.close();
	}
/**
	
	 * localhost:8080/wuyijiu/adminNewsCategory?reqName=getNewsCatesById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsCatesById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		XinWenFenLeiDaoImpl flDao=new XinWenFenLeiDaoImpl();//产品分类的实现类对象
		int id = Integer.parseInt(request.getParameter("cId"));
		XinWenFenLei xw = flDao.getXinwenfenleiById(id);
		
		Gson gson = new Gson();
		out.print(gson.toJson(xw));
		
		out.flush();
		out.close();
	}
/**
	
	 * localhost:8080/wuyijiu/adminNewsCategory?reqName=updateNewsCatesById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateNewsCatesById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		XinWenFenLeiDaoImpl flDao=new XinWenFenLeiDaoImpl();//产品分类的实现类对象
		
		int xinWenId = Integer.parseInt(request.getParameter("xinwenId"));
		String xinWenFenLei = request.getParameter("xinwenFenLei");
		
		List<XinWenFenLei> pcList = flDao.getXinWenByname(xinWenFenLei);
		if (pcList.size() == 0) {
			XinWenFenLei xw = new XinWenFenLei(xinWenId,xinWenFenLei);
			int n = flDao.updateXinwenfenlei(xw);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else {
			out.print(Result.toClient(false, "分类名已存在", ""));
		}
		
		
		out.flush();
		out.close();
	}
}
